﻿using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
using System.Collections.Generic;

namespace TheJoyOfCode.SLUploader
{
    public class DummyUploader : IUploader
    {
        private IFileUploadView _fileUploadView;

        public DummyUploader(IFileUploadView fileUploadView)
        {
            _fileUploadView = fileUploadView;
        }
        
        public void UploadFilesAsync(Queue<FileInfo> files)
        {
            ThreadPool.QueueUserWorkItem(ignored => UploadFiles(files));
        }

        private void UploadFiles(Queue<FileInfo> files)
        {
            int total = files.Count;
            int counter = 0;
            int uploaded = 0;
            while (files.Count > 0)
            {
                FileInfo fileInfo = files.Dequeue();
                OnFileUploadStarted(fileInfo);
                Thread.Sleep(550);
                if (counter % 3 == 2)
                {
                    if (_fileUploadView.ConfirmOverwrite(fileInfo))
                    {
                        uploaded++;
                    }
                }
                else
                {
                    uploaded++;
                }
                counter++;
                OnFileUploadComplete(fileInfo, uploaded, total);
            }
            OnUploadsComplete(uploaded, total);
        }

        protected virtual void OnFileUploadStarted(FileInfo fileInfo)
        {
            if (FileUploadStarted != null)
            {
                FileUploadStarted(this, new FileUploadStartedEventArgs()
                {
                    FileInfo = fileInfo
                });
            }
        }

        protected virtual void OnFileUploadComplete(FileInfo fileInfo, int uploadedFiles, int totalFilesToUpload)
        {
            if (FileUploadComplete != null)
            {
                FileUploadComplete(this, new FileUploadedCompletedEventArgs()
                {
                    FileInfo = fileInfo,
                    TotalFilesToUpload = totalFilesToUpload,
                    UploadedFiles = uploadedFiles
                });
            }
        }

        protected virtual void OnUploadsComplete(int uploadedFiles, int totalFilesToUpload)
        {
            if (UploadsComplete != null)
            {
                UploadsComplete(this, new UploadsCompleteEventArgs()
                {
                    UploadedFiles = uploadedFiles,
                    TotalFilesToUpload = totalFilesToUpload
                });
            }
        }

        public event EventHandler<FileUploadedCompletedEventArgs> FileUploadComplete;

        public event EventHandler<FileUploadStartedEventArgs> FileUploadStarted;

        public event EventHandler<UploadsCompleteEventArgs> UploadsComplete;

    }
}
